<?php
	/**
	* ABSoft Viet Nam Functions file
	* For OpenID access in Exponent 2.0pr2
	* @author: trungdt
	* @version: 1.0
	* @since: Jan 21, 2011
	* 
	* All function in this class works with LightOpenID class
	* You can use Google, Yahoo or other OpenID sevices to login
	* 
	* 
	* Database structure
	* CREATE TABLE `hoithanh.com_prayonline`.`po_openid_users` (
	`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	`indentifer` TEXT NOT NULL DEFAULT '',
	`email` TEXT NOT NULL DEFAULT '',
	`first_name` TEXT NOT NULL DEFAULT '',
	`last_name` TEXT NOT NULL DEFAULT ''
	) ENGINE = MYISAM COMMENT = 'This table store all user who is the first time login by openid ';
	* 
	* Change logs
	* Jan 21, 2011 - trungdt - First create 
	* Feb 14, 2011 - trungdt - Add add_tomenu Function
	* Mar 6, 2011  - trungdt - Add menu Duyệt quyền menu
	* Mar 9, 2011  - trungdt - Add support server belt
	*/

	/** ------------------- ----------------------------- ---------------- **/
	if (!defined('EXPONENT')) exit('');
	define("ABOID_SESSION_NAME","aboid_session_cache_name");
	// Mar 9, 2011  - trungdt - Add support server belt
	define("ABOID_SERVER_BELT",0);
	define("ABOID_NEEDED_BELT",0); 
	// Nov 4, 2011 - trungdt - because, we use date_default_timezone_set
	// we, temporary set SERVER BELT and NEEDED BELT to the same
	// Mar 9, 2011  - trungdt - Add support server belt - end
	/** ------------------- ----------------------------- ---------------- **/

	/**
	* Update Identify in db, Add new if not exist . Update in table openid_users
	* @author: trungdt
	* @version: 1.0  
	* @param mixed $obj
	*/
	function aboid_updateIdentify($obj)
	{
		global $db;
		$temp=$db->selectObject("openid_users","indentifer = '{$obj->indentifer}'");
		if (!$temp)
		{
			$db->insertObject($obj,"openid_users");
		}  
		aboid_registerSession($obj);
	}

	/**
	* Register Session logged in by OpenID
	* Push an OpenID User Object into Session cache
	* 
	* @param mixed $obj
	* @author: trungdt
	*/
	function aboid_registerSession($obj){
		exponent_sessions_set(ABOID_SESSION_NAME,$obj); 
	}

	/**
	* Get current OpenID session object
	* @return: openID user object
	* @author: trungdt
	*/
	function aboid_getSession(){
		return exponent_sessions_get(ABOID_SESSION_NAME);
	}

	/**
	* Current user is logged in or not
	* @return: true if logged in, false if not
	*/
	function aboid_isLoggedin()
	{
		if (aboid_getSession()!=null)
		{
			return true;
		} 
		else
		{
			return false;
		}
	}

	function aboid_Logout()
	{
		exponent_sessions_set(ABOID_SESSION_NAME,null);
	}
	
	/**
	* This function generate the navigation menu for Doremi Shop Project
	* 
	* NOTE: This function automatic checking for selected section
	* @author trungdt
	* @since Sep 26, 2011
	* @param $sections Sections parameter from the Nagivation Class show function
	*/
	function __PDoremiShop_Nav_generate($title, $view, $loc){
		global $sections;
		// current selected section
		global $sectionObj;
		$current_section=clone $sectionObj;
		
		// because Exponent dont know that what section contains content for the request to video page, we must set it manual
		if (isset($_REQUEST['action']))
		{
			$action=$_REQUEST['action'];
			if ($action == "_video_main")
			{
				global $router;
				$current_section=$router->getSectionObj(13);
			}
		}
		
		// $data is level 1
		$data=array();
		// first, get all root section (sections which parent id = 0)
		foreach ($sections as $k=>$v)
			if (($v->parent == 0) && ($v->public == 1) && ($v->active == 1))
				$data[$k]=clone $v;
		//
					  
		// get all sub menu which is automatically generated by depmodule
		$sub = productsmodule::__PDoremiShop_GenrateMenu($current_section->sef_name);
		// for depmodule support
		$sub3=depmodule::_SectionGenerate(array($current_section));
		array_shift($sub3);
		//
		// and get all others menu created by admin
		$sub2 = navigationmodule::levelTemplate($current_section->id,0);
		$sub=array_merge($sub, $sub3, $sub2);
	
		$template = new template('navigationmodule',$view,$loc);
		$template->assign('level1',$data);
		$template->assign('sections',$sub);
		// Fe 14, 2011 - trungdt - Add some Menu Item - end
		$template->assign('current',$current_section);
		$template->assign('num_sections', count($sub));
		global $user;
		$template->assign('canManage',((isset($user->is_acting_admin) && $user->is_acting_admin == 1) ? 1 : 0));
		$template->assign('moduletitle',$title);
		$template->output();
	}
	
	
	/**
	* This function add more menu into section
	* @return: section
	*/
	function aboid_setMenu($cur_sections)
	{
		// copy first object
		$obj=null;
		foreach ($cur_sections as $k=>$v)
		{
			$obj=clone $v;
			break;
		}
		// for menu Manage
		$manage_id=-1;
		// end of menu manage
		if (exponent_users_isLoggedIn())
		{
            $i18n = exponent_lang_loadFile("default.php");
			// prepare $param variable
			$params=null;
			$loc=null;
			// end of prepare
			// logout
			
            // add menu user info
            $obj->name=$i18n['user_profile'];
			$obj->css_class="user";
			//$params['module']="loginmodule";
			//$params['action']="logout";
			//$obj->link=exponent_core_makeLink($params);
            $obj->link="javascript:void(0)";
            $obj->depth=0;
			$obj->id=rand(0,1000);
			$cur_sections[]=clone $obj;
            // save this id for maser id
            $user_menu_id = $object->id;
            
            // sub menu
            // profile
		    $obj->name=$i18n['edit_profile'];
            $obj->css_class="";
            //$params['module']="loginmodule";
            //$params['action']="logout";
            $obj->link="/".LANG_URL_PREFIX."/profile";
            $obj->depth=1;
            $obj->parent = $user_menu_id;
            $obj->id=rand(0,1000);
            $cur_sections[]=clone $obj;
            
            // Change password
            $obj->name=$i18n['change_password'];
            $obj->css_class="";
            $params['module']="loginmodule";
            $params['action']="changepass";
            $obj->link=exponent_core_makeLink($params);
            $obj->depth=1;
            $obj->parent = $user_menu_id;
            $obj->id=rand(0,1000);
            $cur_sections[]=clone $obj;
		}
		//$cur_sections=duanmodule::MenuGenerate_ForCat($cur_sections);
		// 
		//$cur_sections = depmodule::_SectionGenerate($cur_sections);
		return $cur_sections;
	}

/**
* this function convert server time to user local time
*     
*/
function aboid_localtime_convert($hour, $min, &$ret_hour, &$ret_min)
{
   global $user;
   $time_belt=$user->time_belt;
   
   $server_belt=7; // default is GMT +7
   
   if ($min>4)
   { $min=$min/15; } // div by 15
   $time=$hour*4+$min;
   // to GMT 0
   $time=$time-$server_belt*4;
   // to user local time
   $time+=$time_belt*4;
   if ($time < 0)
   {$time+=96;}
   // 
   $ret_hour=floor($time/4);
   $ret_min=($time % 4)*15;
}

/**
* Get current local time of server (GMT +X configured before)
* @param bool $full_time : =true: get full timestamp ; =false: just time*60+min
*/
function aboid_getCurrentTime($full_time=true)
{
	if ($full_time)
	{
		return (time()+(ABOID_NEEDED_BELT-ABOID_SERVER_BELT)*3600);
	}
	else
	{
		return (date("G")*60+date("i")+(ABOID_NEEDED_BELT-ABOID_SERVER_BELT)*60);
	}
}

?>
